package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

import com.FileURL;

public class search extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public search() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String q = java.net.URLDecoder.decode(request.getParameter("query"),
				"UTF-8");
		String type = java.net.URLDecoder.decode(request.getParameter("type"),
				"UTF-8");
		System.out.println(q);
		String res = "";
		String[] arr = q.split(" ");
		String query = "";
		if (arr.length != 0) {
			query = arr[0];
			for (int i = 1; i < arr.length; i++) {
				query += " or " + arr[i];// 查询表达式
			}
		}
		System.out.println(type);
		try {
			IndexSearcher is = null;
			String path = null;
			
			if (type.equals("ports")) {
				path = FileURL.getPortsURL();
			} else if (type.equals("route")) {
				path = FileURL.getRouteURL();
			} else if (type.equals("custom")) {
				path = FileURL.getCustomURL();
			}
			
			is = new IndexSearcher(path);
			QueryParser queryParser = new QueryParser("contents",
					new StandardAnalyzer());

			Query query1 = new TermQuery(new Term("filename", q));
			Hits hits1 = is.search(query1);
			if (hits1.length() != 0) {
				
				String title = "";
				for (int i = 0; i < hits1.length(); i++) {
					Document doc = hits1.doc(i);
					// res[i] = doc.get("filename");
					title = doc.get("filename");
					out.println("<div style='color:red;'>");
					out.println(title);
					out.println("</div>");
					out.println(doc.get("title"));
				}
				
			} else {

				// Hits hits = is.search(query);
				hits1 = is.search(queryParser.parse(query));
				
				if(hits1.length() != 0){
					String title = "";
					for (int i = 0; i < hits1.length(); i++) {
						Document doc = hits1.doc(i);
						// res[i] = doc.get("filename");
						title = doc.get("filename");
						out.println("<div style='color:red;'>");
						out.println(title);
						out.println("</div>");
						out.println(doc.get("title"));
					}
					System.out.println(title);
					out.println(res);
				}else{
					out.println("<div style='color:red;'>");
					out.println("对不起，找不到与 " + q + " 相关的搜索结果!");
					out.println("</div>");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
